package com.soso.base.google.cron.schedule;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.soso.base.util.html.HttpRequester;
import com.soso.base.util.log.Logger;
import com.soso.base.util.other.DateTools;

public abstract class AbstractScheduleRule implements ScheduleRule{
	
	/**
	 * @param v
	 * @param unit hours|mins|minutes
	 * @return
	 */
	protected int getSecond(int v,String unit){
		if(unit==null||"".equals(unit))
			return v;
		if(unit.equals("hours")){
			return v*60*60;
		}else if(unit.equals("minutes")){
			return v*60;
		}
		return v;
	}
	protected String dateToStr(Date date){
		return DateTools.dateToStringWithFormat(date, "yyyy-MM-dd HH:mm:ss");
	}
	protected Date toDate(String HHmmss){
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd "+HHmmss);
		Date date=new Date();
		String strDate=formatter1.format(date);
		SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date = formatter2.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * 当前时间减去second秒之后是否还在旧时间前面
	 * @param oldDate
	 * @param second
	 * @return
	 */
	protected boolean validateInterval(Date oldDate, int second) {
		if(oldDate==null){
			return true;
		}
		GregorianCalendar gc = new GregorianCalendar();
		gc.add(GregorianCalendar.SECOND, -second);
		if (oldDate.before(gc.getTime())) {
			return true;
		}
		return false;
	}
	protected boolean validateBetween(Date from,Date to){
		Date dateCurrent=new Date();
		if (dateCurrent.after(from)&&dateCurrent.before(to)) {
			return true;
		}
		return false;
	}
	protected void request(String url){
		if(url==null||!url.startsWith("http://"))
			return;
		try{
			HttpRequester.sendGet(url,null,null,null,null);
		}catch(IOException e){
			Logger.warn(e.toString());
		}
	}
	protected String getWeek() {
		Calendar now = Calendar.getInstance();
		String dayOfWeek[] = { "" , "sunday" , "monday" , "tuesday" , "wednesday" , "thursday" , "friday" , "saturday" };
		return dayOfWeek[now.get(Calendar.DAY_OF_WEEK)];
	}

}
